/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.Util;

import Pojo.*;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author An
 */
public class ThiSinhDAO {
    public static List<Thisinh> LayThongTinThiSinhTheoCMND_List(String cmnd_){
        List<Thisinh> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "from Thisinh ts where ts.cmnd = :cmnd_";
            Query query = session.createQuery(hql);
            query.setParameter("cmnd_", cmnd_);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
    public static boolean KiemTraCMNDNayTonTaiChua(String cmnd)
    {
        boolean kq = false;
        List<Thisinh> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try { 
            String hql = "from Thisinh ts where ts.cmnd = :cmnd"; 
            Query query = session.createQuery(hql);
            query.setParameter("cmnd", cmnd);
            ds = query.list(); 
            if(ds.size()>0)
                kq = true;
        } catch (HibernateException ex) { 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return kq; 
    }
    public static boolean ThemThiSinh(Thisinh ts)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(ts);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex.getMessages());
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }
    
    public static Thisinh LayThongTinThiSinhTheoCMND(int cmnd)
    {
        Thisinh ts = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = (Thisinh) session.get(Thisinh.class, cmnd);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ts;
    }
}

